package com.pishou.code.bean;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.pishou.URLConfig;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Product {
    private Long sku;
    private Long spu;
    private String name;
    private Double price;
    private Double discount;
    private Integer stock;
    private String category;
    private List<Comment> comments;
    private List<Comment> video_comment;
    private String[] video_paths;
    private List<String> details;

    public Product(){

    }
    public Product(JSONObject product_json){
        this.sku = product_json.getLong("sku");
        this.name = product_json.getString("name");
        this.category = product_json.getString("category");
        this.price = product_json.getDouble("price");
        this.discount = product_json.getDouble("discount");
        this.stock = product_json.getInteger("stock");
    }

    public void setSku(Long sku){
        this.sku = sku;
    }
    public Long getSku(){
        return this.sku;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setPrice(Double price){
        this.price = price;
    }
    public Double getPrice(){
        return this.price;
    }
    public void setStock(Integer stock){
        this.stock = stock;
    }
    public Integer getStock(){
        return this.stock;
    }
    public void setCategory(String  category){
        this.category = category;
    }
    public String getCategory(){
        return this.category;
    }
    public List<String> getDetails(){ return this.details; }
    public Long getSpu() {
        return spu;
    }
    public List<Comment> getVideo_comment() {
        return video_comment;
    }
    public String[] getVideo_paths() {
        return video_paths;
    }

    public void setComments(List<Comment> comments) {
        this.comments = comments;
        this.video_comment = new ArrayList<>();
        for (Comment comment: this.comments) {
            if (new Integer(1).equals(comment.getVideo())){
                this.video_comment.add(comment);
            }
        }
        this.video_paths = new String[video_comment.size()];
        for (int i = 0; i < video_comment.size();i++){
            this.video_paths[i] = video_comment.get(i).getCommentVideoPath();
        }
    }

    public List<Comment> getComments(){
        return this.comments;
    }
    public Double getDiscount() {
        return discount;
    }

    public void setDetails(List<String> details) {
        this.details = details;
    }

    public void setDetails(JSONObject entity){
        List<String> details = new ArrayList<>();
        JSONArray detail = entity.getJSONArray("details");
        if (detail!=null){
            Iterator detail_it = detail.iterator();
            while (detail_it.hasNext()){
                Integer id = (Integer)detail_it.next();
                details.add(URLConfig.oss_host + "SPU/" + id + ".jpg?x-oss-process=image/resize,m_fixed,h_300");
            }
        }
        JSONArray param = entity.getJSONArray("param");
        if (param!=null){
            Iterator param_it = param.iterator();
            while (param_it.hasNext()){
                Integer id = (Integer)param_it.next();
                details.add(URLConfig.oss_host + "SPU/" + id + ".jpg?x-oss-process=image/resize,m_fixed,h_300");
            }
        }
        this.details = details;
    }

    public Integer getVideoPathCurrent(Integer commentId){
        for (int i = 0 ;i<video_comment.size();i++) {
            if (commentId.equals(video_comment.get(i).getId())){
                return i;
            }
        }
        return  null;
    }

}